package com.eop.managedBean;

import javax.faces.context.FacesContext;

import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.user.util.Browser;

import com.base.utils.IdUtils;
import com.eop.entity.Event;
import com.eop.entity.SpecialInfor;

@ManagedBean(name = "eop_addSpecialInforBean", scope = ManagedBeanScope.REQUEST)
public class AddSpecialInforBean extends BaseSpecialInfoBean {

	@Inject
	private FacesContext context;

	@Accessible
	@SaveState
	private String eventId = FacesContext.getCurrentInstance()
			.getExternalContext().getRequestParameterMap().get("eventId");

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			id = context.getExternalContext().getRequestParameterMap()
					.get("id");
			if (eventId != null && !"".equals(eventId)) {
				id = IdUtils.getUUID();
			} else {
				if (id != null && !"".equals(id)) {
					// 编辑
					sf = spInfoService.getStageById(id);
					this.getSpeInfoProperty(sf);
				}
			}
		}
	}

	@Action(id = "save")
	public void save() {
		// 新增
		if (eventId != null && !"".equals(eventId)) {
			sf = new SpecialInfor();
			this.setSpeInfoProperty();
			Event event = eventService.getEventById(eventId);
			if (event != null)
				sf.setSiEvent(event);
			spInfoService.saveSpecialInfor(sf);
		} else {
			sf = spInfoService.getStageById(id);
			if (sf != null) {
				this.setSpeInfoProperty();
				spInfoService.saveSpecialInfor(sf);
			}
		}
		String script = "closeWindow('spInfo_relation')";
		Browser.execClientScript("window.parent", script);
	}

}
